package com.zero.demo.jms.util;

import com.zero.demo.jms.dao.TemplateDao;
import com.zero.demo.jms.entity.TemplateDto;
import com.zero.demo.jms.util.cache.AbstractEhCache;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author zengzr
 * @desc
 * @Date 2019/5/5 17:48
 */
@Component
@Log4j2
public class StEngineConfigCacheUtils extends AbstractEhCache<TemplateDto> {
    @Autowired
    TemplateDao templateDao;

    @Override
    @PostConstruct
    public void initCache() {
       log.info("======初始化模版数据缓存=====");
        List<TemplateDto> templateDtoList = templateDao.findAll();
        for (TemplateDto dto : templateDtoList){
            putToCache(CODE + dto.getTemplateCode(),dto);
            putToCache(ID + dto.getId(),dto);
        }
        initFlag = true;
    }

    public TemplateDto getTemplateDtoByCode(String code){
        return getByKey(CODE + code);
    }

    public TemplateDto getTemplateDtoById(String id){
        return getByKey(ID + id);
    }
}
